package com.dyz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dyz.domain.SysMenu;
import com.dyz.domain.SysPrivilege;
import com.dyz.domain.SysRolePrivilege;
import com.dyz.mapper.SysRolePrivilegeMapper;
import com.dyz.model.Result;
import com.dyz.model.RolePrivilegesParam;
import com.dyz.service.ISysMenuService;
import com.dyz.service.ISysPrivilegeService;
import com.dyz.service.ISysRolePrivilegeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色权限配置 服务实现类
 * </p>
 *
 * @author dyz
 * @since 2025-04-19
 */
@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege> implements ISysRolePrivilegeService {
    @Resource
    private ISysMenuService sysMenuService;
    @Resource
    private SysRolePrivilegeMapper sysRolePrivilegeMapper;
    @Resource
    private ISysPrivilegeService sysPrivilegeService;
    @Override
    public Result<?> getRolePrivileges(Long roleId) {
        //查询权限菜单
        List<SysMenu> list = sysMenuService.list();
        if (StringUtils.isEmpty(list)) {
            //返回空集合
            return Result.fail(Collections.emptyList());
        }
        List<SysMenu> rootMenus = list.stream().filter(sysMenu -> sysMenu.getParentId() == null).collect(Collectors.toList());
        if (StringUtils.isEmpty(rootMenus)) {
            //返回空集合
            return Result.fail(Collections.emptyList());
        }
        //获取二级菜单
        List<SysMenu> subMenus = new ArrayList<>();
        for (SysMenu rootMenu : rootMenus) {
            subMenus.addAll(getChildren(rootMenu.getId(), list, roleId));
        }
        return Result.success(subMenus);
    }

    @Override
    @Transactional
    public Result<?> grantPrivileges(RolePrivilegesParam param) {
        //先删除角色权限
       Long roleId = param.getRoleId();
       sysRolePrivilegeMapper.delete(new LambdaQueryWrapper<SysRolePrivilege>().eq(SysRolePrivilege::getRoleId,roleId));
       //在添加
        List<Long> privileges = param.getPrivileges();
        if(!CollectionUtils.isEmpty(privileges)){
            for (Long privilege : privileges) {
                SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
                sysRolePrivilege.setRoleId(roleId);
                sysRolePrivilege.setPrivilegeId(privilege);
                sysRolePrivilegeMapper.insert(sysRolePrivilege);
            }
        }
        return Result.success(true);
    }

    /**
     * 查询子菜单
     * @param parentId
     * @param list
     * @param roleId
     * @return
     */
    private List<SysMenu> getChildren(Long parentId, List<SysMenu> list, Long roleId) {
        List<SysMenu>children=new ArrayList<>();
        for (SysMenu sysMenu : list) {
            if (parentId.equals(sysMenu.getParentId())) {
               children.add(sysMenu);
               sysMenu.setChildren(getChildren(sysMenu.getId(), list, roleId));
               //查询权限
                    List<SysPrivilege> privileges = sysPrivilegeService.getRolePrivileges(sysMenu.getId(),roleId);
                    sysMenu.setPrivileges(privileges);
            }
        }
        return children;
    }
}
